const { post } = require("./asserts/js/request")

// app.js
App({
  onLaunch(resp) {
    let this_ = this
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      timeout: 5000,
      success: data => {
        console.log("初始小程序");
        console.log(data);

        post("authCode2Session", {
          code: data.code
        }).then( data => {
          console.log("openId"+ data.authInfo.openid)
          this_.globalData.openId = data.authInfo.openid
        })
      },
      fail: err => {
        console.log(err);
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    //获取胶囊位置，作为全局数据
    var data = wx.getMenuButtonBoundingClientRect();
    this.globalData.top = ((data.top + data.bottom)/2 - 11).toFixed();
    this.globalData.bottom = data.bottom;

    //获取全局数据
    this.init();

    //获取枚举值
    this.getEmun();
  },
  init: function() {
    // let this_ = this;
    // wx.showLoading({
    //   title: '加载中',
    //   icon: "none",
    //   mask: true
    // })
    // setTimeout(function() {
    //   this_.globalData.teacherId = "6";
    //   this_.globalData.orgId = "1";
    //   this_.globalData.studentId = "1000";
    //   wx.hideLoading({
    //     success: (res) => {
    //     },
    //   })
    //   if(this_.indexCallback){
    //     this_.indexCallback(4)
    //   };
    // },500);
  },
  getEmun: function() {
    post("courseLevels", {}).then(data => {
      this.globalData.enum_courseLevels = data.courseLevels;
    });
    post("fullDangceTypes",{}).then(data => {
      this.globalData.enum_courseTypes = data.courseTypes;
    });
    post("courseNatrue",{}).then(data => {
      this.globalData.enum_courseNature = data.courseNature;
    })
    post("courseKind", {}).then(
      data => {
        this.globalData.enum_courseKinds= data.courseKind;
      }
    )
  },
  globalData: {
    userInfo: null,
    top: "",
    bottom: "",
    teacherInfo: {
      // teacherId: 7,
      // orgId: 1009
    },
    studentInfo: {

    },
    openId: "",
    enum_courseLevels: [],
    enum_courseTypes: [],
    enum_courseKinds: [],
    enum_courseNature: []
  }
})
